package com.sx.gongzhong.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sx.gongzhong.R;
import com.sx.gongzhong.activity.ExposureSearchActivity;
import com.sx.gongzhong.adapter.MyFragmentPagerAdapter;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.model.PublicBean;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.pest.IntegralPest;
import com.sx.gongzhong.pest.PublicPest;
import com.sx.gongzhong.view.IPublicView;
import com.sx.gongzhong.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by XZY on 2017/12/28.
 * 爆料
 */

public class ExposureFragment extends LazyFragment implements IPublicView{

    @BindView(R.id.im_Search)
    ImageView im_Search;
    @BindView(R.id.tl_TabLayout)
    TabLayout tl_TabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> fragments;
    private boolean isPrepared=false;
    private Disposable disposable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exposure,container,false);
        ButterKnife.bind(this,view);
        isPrepared=true;
        lazyLoad();
        initRx();
        return view;
    }

    private void initRx() {
        disposable = MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent msgEvent) throws Exception {
                        if ("ExposureFragment".equals(msgEvent.getRequest())) {
                            getData();
                        }
                    }
                });
    }

    private void getData() {
        LoadDialog.show(getActivity());
        new PublicPest(this).getPublicList("brokeNewsType");
    }

    @OnClick({R.id.im_Search})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.im_Search:
                startActivity(new Intent(getActivity(), ExposureSearchActivity.class));
                break;
        }
    }

    private void initView(List<PublicBean> beanList) {
        List<String> titles=new ArrayList<>();
        titles.add("全部");
        fragments=new ArrayList<>();
        fragments.add(new ExposureAllFragment());
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(0)));
        tl_TabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i=0;i<beanList.size();i++){
            PublicBean bean=beanList.get(i);
            titles.add(bean.getName());
            Bundle bundle=new Bundle();
            bundle.putSerializable("Type",bean);
            ExposureAllFragment allFragment=new ExposureAllFragment();
            allFragment.setArguments(bundle);
            fragments.add(allFragment);
            tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(i+1)));
        }
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        //tabLayout加载viewpager
        tl_TabLayout.setupWithViewPager(viewPager);
    }


    @Override
    public void showError(String message) {
        LoadDialog.dismiss(getActivity());
    }

    @Override
    public void getPublicList(List<PublicBean> beanList) {
        LoadDialog.dismiss(getActivity());
        if (beanList!=null){
            initView(beanList);
        }
    }

    @Override
    public void onDestroyView() {
        isPrepared=false;
        if (disposable != null){
            disposable = null;
        }
        super.onDestroyView();
    }

    @Override
    protected void lazyLoad() {
        if(!isPrepared || !isVisible) {
            return;
        }
        if (fragments==null){
           getData();
        }
    }
}
